package com.kgc.service.Impl.admin;

import com.kgc.dao.mapper.admin.AdminMapper;
import com.kgc.dao.mapper.user.UserMapper;
import com.kgc.entity.User;
import com.kgc.service.interf.admin.AdminService;
import com.kgc.util.SecretUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * @author hyj
 * @time 2024/4/3 10:15
 * @describe
 */
@Service("adminService")
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public User adminLogin(String username, String password) throws NoSuchAlgorithmException {
        Integer i1 = userMapper.selectByUsername(username);
        Integer i2 = userMapper.selectByEmail(password);
        if (i1 == null && i2 == null) {
            // 用户名/邮箱 不存在
            return new User(-1);
        }
        Integer[] i3 = userMapper.selectByEmailAndUsername(username, SecretUtil.md5(password));
        if (i3.length == 0) {
            // 密码错误
            return new User(-2);
        }
        Integer i4 = userMapper.selectStatus(username);
        if (i4 == 0) {
            return new User(-3);
        }
        return adminMapper.adminLogin(username, SecretUtil.md5(password));
    }

    @Override
    public List<User> getAllUser() {
        return adminMapper.getAllUser();
    }

    @Override
    public Integer updateIntegral(Integer id, Integer integral) {
        return adminMapper.updateIntegral(id, integral);
    }

    @Override
    public Integer updateStatus(Integer id, Integer status) {
        return adminMapper.updateStatus(id, status);
    }

    @Override
    public Integer cancelUser(Integer id) {
        return adminMapper.cancelUser(id);
    }
}
